package com.kaibes.module.account;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kaibes.module.account.core.AccountService;
import com.kaibes.module.account.pojo.Account;
import com.kaibes.module.account.pojo.AccountPost;
import com.kaibes.module.account.pojo.AccountUpdate;
import com.kaibes.web.api.ApiLink;
import com.kaibes.web.api.ResponseApi;
import com.kaibes.web.response.ResponseFactory;

@RestController
@RequestMapping("/accounts")
public class AccountApi extends ResponseApi<Account, AccountService> {
    
    @Autowired
    private PasswordEncoder passwordEncoder;
    
    @ExceptionHandler(value = SQLIntegrityConstraintViolationException.class)
    public ResponseEntity<Object> SQLIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException e) {
        String message = e.getMessage();
        if (message.contains("Duplicate entry")) {
            if (message.contains("username")) {
                return ResponseFactory.conflict("用户名已存在");
            } else {
                return ResponseFactory.conflict("操作失败！");
            }
        } else {
            e.printStackTrace();
            return ResponseFactory.badRequest("操作失败！");
        }
    }
    
    @PostMapping
    @ApiLink("创建账号")
    public ResponseEntity<List<Number>> create(@RequestBody @Validated AccountPost data) {
        data.setPassword(passwordEncoder.encode(data.getPassword()));
        return super.post(data, "创建账号成功", "创建账号失败");
    }
    
    @PutMapping("/{id:\\d+}")
    @ApiLink("修改密码")
    public ResponseEntity<Object> update(@PathVariable Integer id, @RequestBody @Validated AccountUpdate data) {
        data.setPassword(passwordEncoder.encode(data.getPassword()));
        return super.putById(data, id, "账号密码更新成功", "账号密码更新失败");
    }
    
    @PostMapping("/register")
    @ApiLink(value = "注册用户", validated = false)
    public ResponseEntity<List<Number>> register(@RequestBody @Validated AccountPost data) {
        data.setPassword(passwordEncoder.encode(data.getPassword()));
        return super.post(data, "用户注册成功", "用户注册失败");
    }
    
    @PutMapping("/self")
    @ApiLink(value = "修改自己的密码", validated = false)
    public ResponseEntity<Object> updateSelf(@RequestAttribute Integer userId, @RequestBody @Validated AccountUpdate data) {
        data.setPassword(passwordEncoder.encode(data.getPassword()));
        return ResponseFactory.put(service.updatePasswordByUserId(data.getPassword(), userId), "账号密码更新成功", "账号密码更新失败");
    }
    
    @GetMapping("/{id:\\d+}")
    @ApiLink(value = "获取账号信息", validated = false)
    public ResponseEntity<Object> get(@PathVariable Integer id) {
        Account account = service.getById(id);
        if (account != null) {
            account.setPassword(null);
        }
        return ResponseFactory.get(account, "获取账号信息成功", "获取账号信息失败");
    }
    
}
